package py.progweb.sgc.gui.client.widget;

import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

/**
 * Toolbar para los listados
 * 
 * @author jtalavera
 *
 */
public class ToolbarListado extends ToolStrip {
	
	public static int TOOLBAR_BOTON_NUEVO = 0;
	public static int TOOLBAR_BOTON_EDITAR = 1;
	public static int TOOLBAR_BOTON_BORRAR = 2;
	
	final ToolbarListener listener;
	
	public ToolbarListado(ToolbarListener listener) {
		this(listener, new int []{TOOLBAR_BOTON_NUEVO,TOOLBAR_BOTON_EDITAR,TOOLBAR_BOTON_BORRAR});
	}
	
	public ToolbarListado(ToolbarListener listener, int [] botones, ToolStripButton... otrosBotones) {
		ToolStripButton btnNuevo;
		ToolStripButton btnEditar;
		ToolStripButton btnBorrar;
		
		this.listener = listener;
		this.setWidth100();
		
		if (botones != null) {
			for (int btn : botones) {
				if (btn == TOOLBAR_BOTON_NUEVO) {
					 btnNuevo = new ToolStripButton();
					 btnNuevo.setIcon("/sgc-web/images/new.gif");
					 btnNuevo.setTitle("Nuevo");
					 this.addButton(btnNuevo);			
					 
					 btnNuevo.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							ToolbarListado.this.listener.nuevo();
						}
					 });
					 
				} else if (btn == TOOLBAR_BOTON_EDITAR) {
					 btnEditar = new ToolStripButton();
					 btnEditar.setIcon("/sgc-web/images/edit.gif");
					 btnEditar.setTitle("Modificar");
					 this.addButton(btnEditar);
					 
					 btnEditar.addClickHandler(new ClickHandler() {
							public void onClick(ClickEvent event) {
								ToolbarListado.this.listener.modificar();
							}
					 });
					 
				} else if (btn == TOOLBAR_BOTON_BORRAR) {
					 btnBorrar = new ToolStripButton();
					 btnBorrar.setIcon("/sgc-web/images/del.gif");
					 btnBorrar.setTitle("Borrar");
					 this.addButton(btnBorrar);
					 
					 btnBorrar.addClickHandler(new ClickHandler() {
							public void onClick(ClickEvent event) {
								ToolbarListado.this.listener.eliminar();
							}
					});
	
				}
			}
		}
		
		if (otrosBotones.length > 0 && botones != null && botones.length > 0) {
			this.addSeparator();
			
			for(ToolStripButton btn : otrosBotones) 
				this.addButton(btn);
		}
	}
}
